import { Component, EventEmitter, Input, Output, OnChanges } from '@angular/core';
import { Task } from '../../_common/_model/task';
import * as moment from 'moment';
import { UpdateTaskDto } from '../../_common/_dtos/updateTaskDto';
import { TaskService } from '../../_common/_service/task.service';
import { ListService } from '../../_common/_service/list.service';
import { NzMessageService } from 'ng-zorro-antd';

@Component({
  selector: 'app-task-detail',
  templateUrl: './detail.component.html',
  styleUrls: ['./detail.component.less']
})
export class DetailComponent implements OnChanges {
  @Input() currentTask: Task;
  @Output() onHideDetail = new EventEmitter<boolean>();
  creationTime: string;

  constructor(
    private _taskService: TaskService,
    private _listService: ListService,
    private _message: NzMessageService ) {}

  ngOnChanges() {
    const temp = this.currentTask && this.currentTask.creationTime;
    this.creationTime = moment(temp).format('创建于 MMMMDo, dddd');
  }

  toggleToday() {
    const { listId, index, today } = this.currentTask;
    const dto: UpdateTaskDto = {
      listId,
      index,
      ['today']: !today
    };
    this._taskService.updateTask(dto)
      .subscribe(
      next => {
        this._listService.getTasksByListId(listId)
          .find(u => u.index === index)
          .today = !today;
      },
      err => {
        this._message.error(err.msg);
      }
      );
  }

  toggleToDone(index: number) {
  }

  hideDetail() {
    this.onHideDetail.emit(true);
  }

}
